//
//  MakeSureViewController.swift
//  DriverSchoolAPP
//
//  Created by Logel on 2017/5/18.
//  Copyright © 2017年 Logel. All rights reserved.
//

import UIKit

class MakeSureViewController: UIViewController,LgCallBack {
    var perOrder = PreOrder()
    var payFor = ""
    var orderId = ""
    var payType = 0
    @IBOutlet weak var simg1: UIImageView!
    @IBOutlet weak var simg2: UIImageView!
    @IBOutlet weak var simg3: UIImageView!
    @IBOutlet weak var priceLbl: UILabel!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        print(perOrder)
        priceLbl.text = "合计：￥\(perOrder.totalPrice)"
        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    @IBAction func toSuccess(_ sender: UIButton) {
       // self.performSegue(withIdentifier: "toOrderSuccess", sender: nil)
        if payFor == "" {
            switch payType {
            case 1:
                asyn_req_post(method: M_Signalipay, cb: self, p: ["orderIds":perOrder.orderIds.toJSONString() as AnyObject,"accountId":user.accountId as AnyObject])
            case 2:
                asyn_req_post(method: M_SignweixinPay, cb: self, p: ["orderIds":perOrder.orderIds.toJSONString() as AnyObject,"accountId":user.accountId as AnyObject])
            case 3:
                asyn_req_post(method: M_CashPay, cb: self, p: ["orderIds":perOrder.orderIds.toJSONString() as AnyObject,"accountId":user.accountId as AnyObject])
            default:
                break
            }
        }else{
            switch payType {
            case 1:
                asyn_req_post(method: M_Otheralipay, cb: self, p: ["orderIds":perOrder.orderIds.toJSONString() as AnyObject,"accountId":user.accountId as AnyObject])
            case 2:
                asyn_req_post(method: M_Otheralipay, cb: self, p: ["orderIds":perOrder.orderIds.toJSONString() as AnyObject,"accountId":user.accountId as AnyObject])
            case 3:
                showMsg(msg: "课时付费暂时不支持线下支付。")
            default:
                break
            }
        }
    }

    @IBAction func doSelectPay(_ sender: UIButton) {
            payType = sender.tag
            switch sender.tag {
            case 1:
                simg1.image = UIImage(named: "selected")
                simg2.image = UIImage(named: "select")
                simg3.image = UIImage(named: "select")
            case 2:
                simg1.image = UIImage(named: "select")
                simg2.image = UIImage(named: "selected")
                simg3.image = UIImage(named: "select")
            case 3:
                simg1.image = UIImage(named: "select")
                simg2.image = UIImage(named: "select")
                simg3.image = UIImage(named: "selected")
            default:
                break
            }
    }
    
    func callBack(method: String, data: AnyObject) {
        let j = JSON(data)
        print(j)
        let head = parseMobileHead(x: j["MobileHead"])
        if head.code == "1" {
            if payFor == "" {
                switch method {
                case M_CashPay:
                    showMsg(msg: "支付成功，请与报名点老师对接，如果乱点将追究法律责任。")
                    self.performSegue(withIdentifier: "toOrderSuccess", sender: nil)
                default:
                    break
                }
            }
        }else{
            showMsg(msg: "请检查网络，并且确认您的电话身份证没被使用报名。")
            
            self.navigationController?.popToRootViewController(animated: true)
        }
        oList.removeAll()
    }
}
